package com.xigua.字符串;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: 给你一个长桌子，桌子上盘子和蜡烛排成一列。给你一个下标从 0 
 * 开始的字符串 s ，它只包含字符 '*' 和 '|' ，其中 '*' 表示一个 盘子 ，'|' 表示一支 蜡烛 。*
 *同时给你一个下标从 0 开始的二维整数数组 queries ，其中 queries[i] = [lefti, righti]
 *表示 子字符串 s[lefti...righti] （包含左右端点的字符）。对于每个查询，你需要找到 子字符串中 在 两支蜡烛之间 
 * 的盘子的 数目。如果一个盘子在 子字符串中左边和右边都至少有一支蜡烛，那么这个盘子满足在 两支蜡烛之间 。
 *比方说，s = "||**||**|*" ，查询 [3, 8] ，表示的是子字符串 "*||**|" 。
 * 子字符串中在两支蜡烛之间的盘子数目为 2 ，子字符串中右边两个盘子在它们左边和右边 都 至少有一支蜡烛。
 *请你返回一个整数数组 answer ，其中 answer[i] 是第 i 个查询的答案。
 * @date 2022/3/811:58
 */
public class _2055_蜡烛之间的盘子 {


    public static void main(String[] args) {
        String s = "***|**|*****|**||**|*";
        int [][] queries ={{1,17},{4,5},{14,17},{5,11},{15,16}};
        String s1 = "*|*||||**|||||||*||*||*||**|*|*||*";
        int [][] queries1 = {{2,33},{2,32},{3,31},{0,33},{1,24},{3,20},{7,11},{5,32},{2,31},
                {5,31},{0,31},{3,28},{4,33},{6,29},{2,30},{2,28},{1,30},{1,33},{4,32},{5,30},{4,23},
                {0,30},{3,10},{5,28},{0,28},{4,28},{3,33},{0,27}};

        System.out.println(platesBetweenCandles1(s,queries));
    }


    public static int[] platesBetweenCandles(String s, int[][] queries) {
        int length = s.length();
        List<Integer> list = new ArrayList<>();
        int num = 0;
        int start = s.indexOf("|");
        int tep = 0;
        for (int i = 0; i < length; i++) {
            if (i<=start){
                list.add(0);
            }else {
                if (s.charAt(i) == '*'){
                    list.add(tep);
                    num++;
                }else {
                    tep = num;
                    list.add(tep);
                }
            }
        }
        int ans[] = new int[queries.length];
        for (int i = 0; i < queries.length; i++) {
            int index = queries[i][0];
            int end = queries[i][1];
            if (list.get(end) == list.get(index)){
                ans[i] = 0;
            }else {
                while (s.charAt(index) == '*') {
                    index++;
                }
                ans[i] = list.get(end) - list.get(index);
            }
        }
        return ans;
    }


    public static int[] platesBetweenCandles1(String s, int[][] queries) {
        int n = s.length();
        int[] preSum = new int[n];
        for (int i = 0, sum = 0; i < n; i++) {
            if (s.charAt(i) == '*') {
                sum++;
            }
            preSum[i] = sum;
        }
        int[] left = new int[n];
        for (int i = 0, l = -1; i < n; i++) {
            if (s.charAt(i) == '|') {
                l = i;
            }
            left[i] = l;
        }
        int[] right = new int[n];
        for (int i = n - 1, r = -1; i >= 0; i--) {
            if (s.charAt(i) == '|') {
                r = i;
            }
            right[i] = r;
        }
        int[] ans = new int[queries.length];
        for (int i = 0; i < queries.length; i++) {
            int[] query = queries[i];
            int x = right[query[0]], y = left[query[1]];
            ans[i] = x == -1 || y == -1 || x >= y ? 0 : preSum[y] - preSum[x];
        }
        return ans;
    }
}


